﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using Jy123.Common;

namespace Jy123.Web.Module.Club
{
    public partial class CreateModule : System.Web.UI.Page
    {
        private int userId;
        private Jy123.Model.User_Main user;
        protected void Page_Load(object sender, EventArgs e)
        {
            string key = "UserID";
            string userIdString = Cookie.getCookie(key);
            if (string.IsNullOrEmpty(userIdString))
            {
                Response.Redirect("/");
            }
            if (!string.IsNullOrEmpty(userIdString))
            {
                userId = int.Parse(userIdString);
                Jy123.BLL.Venues.VModule BLLVModule = new Jy123.BLL.Venues.VModule();
                Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                Jy123.BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                Jy123.BLL.Module.Module BLLMModule = new Jy123.BLL.Module.Module();
                Jy123.BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();

                List<Jy123.Model.Module_Main> listModule = BLLModule.GetModelList("FUser = " + userId.ToString());
                List<Jy123.Model.Public_SportType> listSportType = BLLSportType.GetModelList(string.Empty);
                Dictionary<int, string> dicSport = new Dictionary<int, string>();
                foreach (Jy123.Model.Public_SportType sportType in listSportType)
                {
                    dicSport.Add(sportType.ID, sportType.Logo);
                }
                List<Jy123.Model.Public_JType> listJType = BLLJType.GetModelList("JFather IS NOT NULL");
                string html = Cache["createmodule"].ToString();
                StringBuilder sbString = new StringBuilder();
                List<Jy123.Model.Public_JType> listResult = new List<Jy123.Model.Public_JType>();
                int mark = 0;
                //列表中添加可用但是用户没有开通的组件
                foreach (Jy123.Model.Public_JType JType in listJType)
                {
                    if (!IsInList(JType.ID, listModule) && JType.Enabled)
                    {
                        listResult.Add(JType);
                        mark += 1;
                    }                    
                }
                //列表中添加可用并且用户已经开通的组件
                foreach (Jy123.Model.Public_JType JType in listJType)
                {
                    if (IsInList(JType.ID, listModule) && JType.Enabled)
                    {
                        listResult.Add(JType);
                    }
                }
                //列表中添加没有开通的组件
                //foreach (Jy123.Model.Public_JType JType in listJType)
                //{
                //    if (!JType.Enabled)
                //    {
                //        listResult.Add(JType);
                //    }                    
                //}
                if (listResult.Count > 0)
                {
                    html = html.Replace("$JTypeId$", listResult[0].ID.ToString());
                }
                else
                {
                    html = html.Replace("$JTypeId$", string.Empty);
                }
                foreach (Jy123.Model.Public_JType JType in listResult)
                {
                    string strLi = Cache["createmoduleli"].ToString();
                    if (!string.IsNullOrEmpty(JType.Model))
                    {
                        strLi = strLi.Replace("$ImgSrc$", JType.Model);
                    }
                    else
                    {
                        strLi = strLi.Replace("$ImgSrc$", "/Images/Module/Module.gif");
                    }
                    strLi = strLi.Replace("$Value$", JType.ID.ToString());
                    strLi = strLi.Replace("$radioId$", "radio" + JType.ID);
                    string strDis = "disabled = 'disabled'";
                    if (!IsInList(JType.ID, listModule) && JType.Enabled)
                    {
                        strDis = string.Empty;
                    }
                    strLi = strLi.Replace("$Disabled$", strDis);
                    strLi = strLi.Replace("$ModuleName$", JType.JName);
                    if (JType.ID == listResult[0].ID)
                    {
                        strLi = strLi.Replace("$Checked$", "checked = 'checked'");
                    }
                    else
                    {
                        strLi = strLi.Replace("$Checked$", string.Empty);
                    }
                    string strClass = string.Empty;
                    if (!JType.Enabled)
                    {
                        strClass = "class='gray'";
                    }
                    strLi = strLi.Replace("$Class$", strClass);
                    sbString.Append(strLi);
                }

                html = html.Replace("$Li$", sbString.ToString());
                html = html.Replace("$UserId$", userId.ToString());
                if (mark > 0)
                {
                    html = html.Replace("$SubDis$", string.Empty);
                }
                else
                {
                    html = html.Replace("$SubDis$", "disabled = 'disabled'");
                }
                Response.Write(html);
            }
            else
            {
                //找不到UserId，返回首页,目前暂时返回空值
            }
        }

        private bool IsInList(int id, List<Jy123.Model.Module_Main> listModule)
        {
            foreach (Jy123.Model.Module_Main module in listModule)
            {
                if (module.FJType.Value == id)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
